package com.taomeishu.mailcrazy.checkers.neteasy;

import org.json.JSONException;
import org.json.JSONObject;

import com.taomeishu.mailcrazy.CheckResult;
import com.taomeishu.mailcrazy.Mail;
import com.taomeishu.mailcrazy.http.ClientResponse;
import com.taomeishu.mailcrazy.parser.ResultParseException;
import com.taomeishu.mailcrazy.parser.ResultParser;

public class NeteasyResultParser implements ResultParser {

	//{message:"true",mail163:true,mail126:true,mailyeah:true,vip163:true,vip126:true,vip188:true}
	
	//{message:"true",mail163:false,mail126:false,mailyeah:false,vip163:false,vip126:true,vip188:true}
	
	@Override
	public CheckResult parse(ClientResponse clientResponse, String username, String domainName) throws ResultParseException {
		String s = new String(clientResponse.getData());
		CheckResult result = new CheckResult(true);
		result.setMail(new Mail(username, domainName));
		try {
			JSONObject json = new JSONObject(s);
			boolean exist163 = !json.getBoolean("mail163");
			boolean exist126 = !json.getBoolean("mail126");
			boolean existYeah = !json.getBoolean("mailyeah");
			if (exist163) {
				result.addExistMail(new Mail(username, "163.com"));
			} else {
				result.addNotExistMail(new Mail(username, "163.com"));
			}
			if (exist126) {
				result.addExistMail(new Mail(username, "126.com"));
			} else {
				result.addNotExistMail(new Mail(username, "126.com"));
			}
			if (existYeah) {
				result.addExistMail(new Mail(username, "yeah.net"));
			} else {
				result.addNotExistMail(new Mail(username, "yeah.net"));
			}
		} catch (JSONException e) {
			throw new ResultParseException(e);
		}
		return result;
	}

}
